package Control;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import Entity.MsgPackage;
import Entity.User;
import GUI.UpdateAuthGui;
import GUI.ScreenManager;
/**
 * Controls the instance of Update authorization
 * This class sends and gets data from the server 
 * and display it on the panel.
 * @author Nofar
 *
 */
public class UpdateAuthControl {
	private UpdateAuthGui upAuthGui;
	private MsgPackage updateAuthMsg;
	private User user;
	
	/**
	 * the constructor
	 */
	public UpdateAuthControl(){
		upAuthGui= new UpdateAuthGui();
		updateAuthMsg= new MsgPackage(); 
		user= new User();
	}
	/**
	 * 
	 * @return UpdateAuthGui
	 */
	public UpdateAuthGui getUpAuthGui(){
		return upAuthGui;
	}
	
	/**
	 * This method sends authrization request to the server
	 *  The server sends an answer (received from query from database) 
	 *  and the control display it in a message
	 * @return boolean
	 */
	public boolean updateAuthorization_pressed(){
		String x =upAuthGui.getTextArea_userName().getText();
		String y =upAuthGui.getTextArea_newAuth().getText();
		if((x==null)||y==null ){
			JOptionPane.showMessageDialog(null, "Not all the fileds all the fill", "ERROR", JOptionPane.ERROR_MESSAGE);
              return false;
		}
		user.setUserName(Integer.parseInt(upAuthGui.getTextArea_userName().getText()));
		user.setAuthorization(upAuthGui.getTextArea_newAuth().getText());
		updateAuthMsg.setObject(user);
		if(y.equals("committee")){
			updateAuthMsg.setOp_code(OP_CODE.UPDATE_AUTHORIZATION_COMM);
			ScreenManager.getConnection().accept(updateAuthMsg);
			JOptionPane.showMessageDialog(null, "The authrization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
			return true;
		}
		else if ((y.equals("supervisor"))||(y.equals("chairman"))){
		updateAuthMsg.setOp_code(OP_CODE.UPDATE_AUTHORIZATION_SUP_OR_CHAIR);
		ScreenManager.getConnection().accept(updateAuthMsg);
		JOptionPane.showMessageDialog(null, "The authrization updated successfully", "", JOptionPane.INFORMATION_MESSAGE);
		return true;
		}
		else 
			JOptionPane.showMessageDialog(null, "illegal authrization", "ERROR", JOptionPane.ERROR_MESSAGE);
        	return false;
			
	}
}
